package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CommunityUnit;
import com.ruoyi.system.service.ICommunityUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * unitController
 * 
 * @author hanpee
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/system/unit")
public class CommunityUnitController extends BaseController
{
    @Autowired
    private ICommunityUnitService communityUnitService;

    /**
     * 查询unit列表
     * 执行顺序确保starPage方法在List查询之前，否则直接显示全部数据不会进行分页
     */
    @PreAuthorize("@ss.hasPermi('system:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityUnit communityUnit)
    {
        startPage();
        List<CommunityUnit> list = communityUnitService.selectCommunityUnitList(communityUnit);
        return getDataTable(list);
    }

    /**
     * 导出unit列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit:export')")
    @Log(title = "unit", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityUnit communityUnit)
    {
        List<CommunityUnit> list = communityUnitService.selectCommunityUnitList(communityUnit);
        ExcelUtil<CommunityUnit> util = new ExcelUtil<CommunityUnit>(CommunityUnit.class);
        util.exportExcel(response, list, "unit数据");
    }

    /**
     * 获取unit详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(communityUnitService.selectCommunityUnitById(id));
    }

    /**
     * 新增unit
     */
    @PreAuthorize("@ss.hasPermi('system:unit:add')")
    @Log(title = "unit", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityUnit communityUnit){
        try {
            return toAjax(communityUnitService.insertCommunityUnit(communityUnit));
        } catch(Exception e) {
            return AjaxResult.error("该栋对应的单元已存在");
        }
    }

    /**
     * 修改unit
     */
    @PreAuthorize("@ss.hasPermi('system:unit:edit')")
    @Log(title = "unit", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunityUnit communityUnit)
    {
        return toAjax(communityUnitService.updateCommunityUnit(communityUnit));
    }

    /**
     * 删除unit
     */
    @PreAuthorize("@ss.hasPermi('system:unit:remove')")
    @Log(title = "unit", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(communityUnitService.deleteCommunityUnitByIds(ids));
    }

    /**
     * 级联删除unit
     * @param id 单元Id
     */
    @DeleteMapping("/del/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        try{
            return toAjax(communityUnitService.deleteCommunityUnitById(id));
        } catch(Exception e) {
            return AjaxResult.error("当前单元下有楼层，请先将当前单元下楼层删除后再试");
        }
    }

    /**
     * 查询所有单元
     * @return 分页数据对象
     */
    @PostMapping("unit")
    public TableDataInfo listUnit(){
        return getDataTable(communityUnitService.listUnit());
    }
}
